package com.sysu.homework.action;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.homework.model.Course;
import com.sysu.homework.model.Student;
import com.sysu.homework.model.TeachAssistant;
import com.sysu.homework.model.Teacher;
import com.sysu.homework.service.CourseService;
import com.sysu.homework.service.StudentService;
import com.sysu.homework.service.TeachAssistantService;
import com.sysu.homework.service.TeacherService;

@Component("addUserAction")
@Scope("prototype")
public class AddUserAction extends ActionSupport {
	private int studentid;
	private String studentname;
	private String password;
	
	private StudentService studentService;
	private CourseService courseService;
	private TeachAssistantService teachAssistantService;
	private TeacherService teacherService;
	

	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/javascript;charset=GBK");
		PrintWriter out = response.getWriter();
		
		if (getStudentService().isExist(studentid)){
			out.println(1);
			return null;
		}
		
		Student student = new Student();
		student.setId(studentid);
		student.setName(studentname);
		student.setPassword(password);
		
		Course course = courseService.getCourseById(1);
		student.setCourse(course);
		
		TeachAssistant ta = teachAssistantService.getTeachAssistantById(11);
		student.setTa(ta);
		
		Teacher teacher = teacherService.getTeacherById(11);
		student.setTeacher(teacher);
		
		if (getStudentService().addStudent(student) > 0){
			out.println(2);
			return null;
		}
			
		out.println(0);
		return null;
	}
	public TeachAssistantService getTeachAssistantService() {
		return teachAssistantService;
	}

	@Resource
	public void setTeachAssistantService(TeachAssistantService teachAssistantService) {
		this.teachAssistantService = teachAssistantService;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	
	public int getStudentid() {
		return studentid;
	}

	public void setStudentid(int studentid) {
		this.studentid = studentid;
	}

	public String getStudentname() {
		return studentname;
	}

	public void setStudentname(String studentname) {
		this.studentname = studentname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public CourseService getCourseService() {
		return courseService;
	}

	

}
